package com.irun.sm.ui.demo.view;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.HorizontalScrollView;

/***
 * @author huangsm
 * @date 2013-4-1
 * @email huangsanm@gmail.com
 * @desc 
 */
public class MyHorizontalScrollView extends HorizontalScrollView {

	public MyHorizontalScrollView(Context context) {
		super(context);
		init();
	}
	
	public MyHorizontalScrollView(Context context, AttributeSet attr) {
		super(context, attr);
		init();
	}
	
	public MyHorizontalScrollView(Context context, AttributeSet attr, int defStyle) {
		super(context, attr, defStyle);
		init();
	}
	
	private void init(){
		setHorizontalFadingEdgeEnabled(false);
		setVerticalFadingEdgeEnabled(false);
	}
	
	//重置布局
	public void resetView(View[] child, int scrollIndex, SizeCallBack sizeCallBack){
		ViewGroup parent = (ViewGroup) getChildAt(0);
		for (int i = 0; i < child.length; i++) {
			child[i].setVisibility(View.INVISIBLE);
			parent.addView(child[i]);
		}
		
		//当layout的view改变时执行的回调方法
		OnGlobalLayoutListener listener = new MyOnGlobalLayoutListener(parent, child, scrollIndex, sizeCallBack);
		getViewTreeObserver().addOnGlobalLayoutListener(listener);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		return false;
	}
	
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		return false;
	}

	
	class MyOnGlobalLayoutListener implements OnGlobalLayoutListener {

		private ViewGroup mParent;
		private View[] mChild;
		private int mScrollIndex;
		private SizeCallBack mSizeCallBack;
		private int mScrollPos;
		
		public MyOnGlobalLayoutListener(ViewGroup parent, View[] child, int scrollIndex, SizeCallBack sizeCallback){
			mParent = parent;
			mChild = child;
			mScrollIndex = scrollIndex;
			mSizeCallBack = sizeCallback;
		}
		
		@Override
		public void onGlobalLayout() {
			System.out.println("onGlobalLayout...");
			final HorizontalScrollView me = MyHorizontalScrollView.this;
			//移除监听
			me.getViewTreeObserver().removeGlobalOnLayoutListener(this);
			mSizeCallBack.onMeasureView();
			mParent.removeViewsInLayout(0, mChild.length);
			
			final int w = me.getMeasuredWidth();
			final int h = me.getMeasuredHeight();
			
			System.out.println("w:" + w + ",h:" + h);
			int[] dimens = new int[2];
			mScrollPos = 0;
			for (int i = 0; i < mChild.length; i++) {
				mSizeCallBack.getViewSize(i, w, h, dimens);
				mChild[i].setVisibility(View.VISIBLE);
				mParent.addView(mChild[i], dimens[0], dimens[1]);
				if(i < mScrollIndex){
					mScrollPos += dimens[0];
				}
			}
			
			new Handler().post(new Runnable() {
				@Override
				public void run() {
					me.scrollBy(mScrollPos, 0);
				}
			});
			
		}
	}
	
	public interface SizeCallBack {
		/**
		 * 测量view
		 */
		public void onMeasureView();
		
		/**
		 * @param index
		 * @param w
		 * @param h
		 * @param dims
		 */
		public void getViewSize(int index, int w, int h, int[] dims);
		
	}
}
